import { _decorator, Component, Node, Enum, tween, Vec3, UIOpacity, v3 } from 'cc';
const { ccclass, property } = _decorator;

export enum actionType{
    SCALE = 0,
    ROTATE = 1,
    HideShow = 2,
    LeftRight = 3
}

@ccclass('HandMediator')
export class HandMediator extends Component {

    @property({type : Enum(actionType)})
    curAction : actionType = actionType.SCALE;

    @property({displayName : "缩放的最大值"})
    maxScale : number = 0.1;

    @property({displayName : "旋转的速度"})
    rotateSpeed : number = 1;

    @property({displayName : "左右移动得距离"})
    maxLength : number = 200;
 
    start() {
        this.playAnimationAction(this.curAction);
    }

    playAnimationAction(actionIndex){
        switch (actionIndex) {
            case 0:
                this.scaleAction();
                break;
            case 1:
                this.rotateAction();
                break;
            case 2:
                this.hideAndShowAction();
                break;
            case 3:
                this.leftAndRightAction();
                break;
        }
    }

    rotateAction(){
        tween(this.node)
        .repeatForever(
            tween(this.node)
                .by(this.rotateSpeed,{angle : 360})
        )
        .start();
    }

    scaleAction(){
        let initScale = new Vec3(this.node.scale.x,this.node.scale.y,0);
        tween(this.node)
            .to(0.5,{
                scale : new Vec3(initScale.x + this.maxScale,initScale.y + this.maxScale,initScale.z)})
            .to(0.5,{
                scale : initScale
            })
            .call(()=>{
                this.scaleAction();
            })
            .start();
    }

    hideAndShowAction(){
        let node_opacity = this.node.getComponent(UIOpacity);
        tween(node_opacity)
            .to(0.5,{
                opacity : 0
            })
            .to(0.5,{
                opacity : 255
            })
            .call(()=>{
                this.hideAndShowAction();
            })
            .start();
    }

    isMovingDir = 0;
    leftAndRightAction(){
        let pos = this.node.position;
        if(this.isMovingDir == 0){
            let distance = Vec3.distance(pos,v3(-this.maxLength,pos.y,pos.z));
            tween(this.node)
                .to(distance / 200,{
                    position : v3(-this.maxLength,pos.y,pos.z)})
                .call(()=>{
                    this.isMovingDir++;
                    this.isMovingDir = this.isMovingDir % 2;
                    this.leftAndRightAction();
                })
                .start();
        }else{
            let distance = Vec3.distance(pos,v3(this.maxLength,pos.y,pos.z));
            tween(this.node)
                .to(distance / 200,{
                    position : v3(this.maxLength,pos.y,pos.z)})
                .call(()=>{
                    this.isMovingDir++;
                    this.isMovingDir = this.isMovingDir % 2;
                    this.leftAndRightAction();
                })
                .start();
        }
    }
}

